package com.deliveryservicesystem.presentation.desktop;

import com.deliveryservicesystem.app.exceptions.NoRowSelected;
import com.deliveryservicesystem.app.exceptions.NonexistentEntityException;
import com.deliveryservicesystem.app.exceptions.OutputError;
import com.deliveryservicesystem.app.util.AppUtils;
import com.deliveryservicesystem.app.util.ExcelExporter;
import com.deliveryservicesystem.presentation.desktop.addpanel.*;
import com.deliveryservicesystem.presentation.desktop.report.ReportFrame;
import com.deliveryservicesystem.service.crud.impl.*;
import java.awt.HeadlessException;
import java.awt.print.PrinterException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.SingleSelectionModel;
import javax.swing.table.TableModel;
import jxl.write.WriteException;

/**
 * File Name: MainFrame.java
 * Version:
 * Description:
 * Package: com.deliveryservicesystem.presentation.desktop
 * Author: Lance
 * Date: Jun 19, 2012
 * Editor:
 */
public class MainFrame extends javax.swing.JFrame
{

    private static final long serialVersionUID = 1L;
    private MainPanel billingPanel;
    private MainPanel customerPanel;
    private MainPanel deliveryPanel;
    private MainPanel driverPanel;
    private MainPanel productPanel;
    private MainPanel routePanel;
    private MainPanel subscriptionPanel;
    private MainPanel supplierPanel;

    /**
     * Creates new form MainFrame
     */
    public MainFrame()
    {
        initializeLookAndFeels();
        initComponents();
        pack();
        setTitle("Delivery System");
        AppUtils.locateOnScreenCenter(this);
        runFrame();
    }

    /**
     * @param args the command line arguments
     */
    private void runFrame()
    {
        try
        {
            billingPanel = new MainPanel("BEGIN ? := GET_DATA.BILLINGS; END;");
            tabbedPane.add("BILLINGS", billingPanel);
            customerPanel = new MainPanel("BEGIN ? := GET_DATA.CUSTOMERS; END;");
            tabbedPane.add("CUSTOMERS", customerPanel);
            deliveryPanel = new MainPanel("BEGIN ? := GET_DATA.DELIVERIES; END;");
            tabbedPane.add("DELIVERIES", deliveryPanel);
            driverPanel = new MainPanel("BEGIN ? := GET_DATA.DRIVERS; END;");
            tabbedPane.add("DRIVERS", driverPanel);
            productPanel = new MainPanel("BEGIN ? := GET_DATA.PRODUCTS; END;");
            tabbedPane.add("PRODUCTS", productPanel);
            routePanel = new MainPanel("BEGIN ? := GET_DATA.ROUTES; END;");
            tabbedPane.add("ROUTES", routePanel);
            subscriptionPanel = new MainPanel("BEGIN ? := GET_DATA.SUBSCRIPTIONS; END;");
            tabbedPane.add("SUBSCRIPTIONS", subscriptionPanel);
            supplierPanel = new MainPanel("BEGIN ? := GET_DATA.SUPPLIERS; END;");
            tabbedPane.add("SUPPLIERS", supplierPanel);
        }
        catch (SQLException ex)
        {
            OutputError.toLog(ex);
            OutputError.toUser(ex);
        }
        this.setVisible(true);
    }

    private void initializeLookAndFeels()
    {
        //"Metal", "Nimbus", "CDE/Motif", "Windows", "Windows Classic"
        try
        {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
            {
                if ("Nimbus".equals(info.getName()))
                {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        }
        catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex)
        {
            OutputError.toLog(ex);
        }
    }
    
    private void showDialog(String frameName, JDialog panel) throws HeadlessException
    {
        panel.setTitle(frameName);
        AppUtils.locateOnScreenCenter(panel);
        panel.setVisible(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabbedPane = new javax.swing.JTabbedPane();
        lblStatusBar = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jPanel2 = new javax.swing.JPanel();
        btnAdd = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnPrint = new javax.swing.JButton();
        btnExport = new javax.swing.JButton();
        jSeparator5 = new javax.swing.JSeparator();
        jSeparator6 = new javax.swing.JSeparator();
        btnReport = new javax.swing.JButton();
        btnExit = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tabbedPane.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                tabbedPaneStateChanged(evt);
            }
        });

        lblStatusBar.setText("lblStatusBar");

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Controles", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, java.awt.Color.black));

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/toolbarButtonGraphics/general/Add16.gif"))); // NOI18N
        btnAdd.setMnemonic('a');
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/toolbarButtonGraphics/general/Edit16.gif"))); // NOI18N
        btnUpdate.setMnemonic('u');
        btnUpdate.setText("Update");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/toolbarButtonGraphics/general/Delete16.gif"))); // NOI18N
        btnDelete.setMnemonic('d');
        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        btnPrint.setIcon(new javax.swing.ImageIcon(getClass().getResource("/toolbarButtonGraphics/general/Print16.gif"))); // NOI18N
        btnPrint.setMnemonic('p');
        btnPrint.setText("Print");
        btnPrint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrintActionPerformed(evt);
            }
        });

        btnExport.setIcon(new javax.swing.ImageIcon(getClass().getResource("/toolbarButtonGraphics/general/Export16.gif"))); // NOI18N
        btnExport.setMnemonic('e');
        btnExport.setText("Export");
        btnExport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExportActionPerformed(evt);
            }
        });

        btnReport.setIcon(new javax.swing.ImageIcon(getClass().getResource("/toolbarButtonGraphics/general/Properties16.gif"))); // NOI18N
        btnReport.setMnemonic('r');
        btnReport.setText("Report");
        btnReport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReportActionPerformed(evt);
            }
        });

        btnExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/toolbarButtonGraphics/general/Stop16.gif"))); // NOI18N
        btnExit.setMnemonic('x');
        btnExit.setText("Exit");
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator5)
            .addComponent(jSeparator6)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnDelete, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnExport, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnReport, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnExit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(btnPrint, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnUpdate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnAdd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnAdd)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnUpdate)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnDelete)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator6, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnPrint)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnExport)
                .addGap(11, 11, 11)
                .addComponent(jSeparator5, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnReport)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnExit)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jMenu1.setText("File");

        jMenuItem1.setText("jMenuItem1");
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");

        jMenuItem2.setText("jMenuItem2");
        jMenu2.add(jMenuItem2);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(lblStatusBar))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 705, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 490, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblStatusBar))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tabbedPaneStateChanged(javax.swing.event.ChangeEvent evt)//GEN-FIRST:event_tabbedPaneStateChanged
    {//GEN-HEADEREND:event_tabbedPaneStateChanged
        SingleSelectionModel tabbedPaneModel = tabbedPane.getModel();
        if (tabbedPaneModel.getSelectedIndex() == Tab.BILLING.getCode())
        {
            billingPanel.refreshTable();
        }
        else if (tabbedPaneModel.getSelectedIndex() == Tab.CUSTOMER.getCode())
        {
            customerPanel.refreshTable();
        }
        else if (tabbedPaneModel.getSelectedIndex() == Tab.DELIVERY.getCode())
        {
            deliveryPanel.refreshTable();
        }
        else if (tabbedPaneModel.getSelectedIndex() == Tab.DRIVER.getCode())
        {
            driverPanel.refreshTable();
        }
        else if (tabbedPaneModel.getSelectedIndex() == Tab.PRODUCT.getCode())
        {
            productPanel.refreshTable();
        }
        else if (tabbedPaneModel.getSelectedIndex() == Tab.ROUTE.getCode())
        {
            routePanel.refreshTable();
        }
        else if (tabbedPaneModel.getSelectedIndex() == Tab.SUBSCRIPTION.getCode())
        {
            subscriptionPanel.refreshTable();
        }
        else if (tabbedPaneModel.getSelectedIndex() == Tab.SUPPLIER.getCode())
        {
            supplierPanel.refreshTable();
        }
    }//GEN-LAST:event_tabbedPaneStateChanged

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnAddActionPerformed
    {//GEN-HEADEREND:event_btnAddActionPerformed
        SingleSelectionModel tabbedPaneModel = tabbedPane.getModel();

        if (tabbedPaneModel.getSelectedIndex() == Tab.BILLING.getCode())
        {
            showDialog("Add Billing", new AddEditBillingDialog(this, true, new HashMap<String, String>()));
            billingPanel.refreshTable();
        }
        else if (tabbedPaneModel.getSelectedIndex() == Tab.CUSTOMER.getCode())
        {
            showDialog("Add Customer", new AddEditCustomerDialog(this, true, new HashMap<String, String>()));
            customerPanel.refreshTable();
        }
        else if (tabbedPaneModel.getSelectedIndex() == Tab.DELIVERY.getCode())
        {
            showDialog("Add Delivery", new AddEditDeliveryDialog(this, true, new HashMap<String, String>()));
            deliveryPanel.refreshTable();
        }
        else if (tabbedPaneModel.getSelectedIndex() == Tab.DRIVER.getCode())
        {
            showDialog("Add Driver", new AddEditDriverDialog(this, true, new HashMap<String, String>()));
            driverPanel.refreshTable();
        }
        else if (tabbedPaneModel.getSelectedIndex() == Tab.PRODUCT.getCode())
        {
            showDialog("Add Product", new AddEditProductDialog(this, true, new HashMap<String, String>()));
            productPanel.refreshTable();
        }
        else if (tabbedPaneModel.getSelectedIndex() == Tab.ROUTE.getCode())
        {
            showDialog("Add Route", new AddEditRouteDialog(this, true, new HashMap<String, String>()));
            routePanel.refreshTable();
        }
        else if (tabbedPaneModel.getSelectedIndex() == Tab.SUBSCRIPTION.getCode())
        {
            showDialog("Add Subscription", new AddEditSubscritpionDialog(this, true, new HashMap<String, String>()));
            subscriptionPanel.refreshTable();
        }
        else if (tabbedPaneModel.getSelectedIndex() == Tab.SUPPLIER.getCode())
        {
            showDialog("Add Supplier", new AddEditSupplierDialog(this, true, new HashMap<String, String>()));
            supplierPanel.refreshTable();
        }
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnUpdateActionPerformed
    {//GEN-HEADEREND:event_btnUpdateActionPerformed
        SingleSelectionModel tabbedPaneModel = tabbedPane.getModel();
        try
        {
            if (tabbedPaneModel.getSelectedIndex() == Tab.BILLING.getCode())
            {
                showDialog("Edit Billing", new AddEditBillingDialog(this, true, billingPanel.getSelectedRow()));
                billingPanel.refreshTable();
            }
            else if (tabbedPaneModel.getSelectedIndex() == Tab.CUSTOMER.getCode())
            {
                showDialog("Edit Customer", new AddEditCustomerDialog(this, true, customerPanel.getSelectedRow()));
                customerPanel.refreshTable();
            }
            else if (tabbedPaneModel.getSelectedIndex() == Tab.DELIVERY.getCode())
            {
                showDialog("Edit Delivery", new AddEditDeliveryDialog(this, true, deliveryPanel.getSelectedRow()));
                deliveryPanel.refreshTable();
            }
            else if (tabbedPaneModel.getSelectedIndex() == Tab.DRIVER.getCode())
            {
                showDialog("Edit Driver", new AddEditDriverDialog(this, true, driverPanel.getSelectedRow()));
                driverPanel.refreshTable();
            }
            else if (tabbedPaneModel.getSelectedIndex() == Tab.PRODUCT.getCode())
            {
                showDialog("Edit Product", new AddEditProductDialog(this, true, productPanel.getSelectedRow()));
                productPanel.refreshTable();
            }
            else if (tabbedPaneModel.getSelectedIndex() == Tab.ROUTE.getCode())
            {
                showDialog("Edit Route", new AddEditRouteDialog(this, true, routePanel.getSelectedRow()));
                routePanel.refreshTable();
            }
            else if (tabbedPaneModel.getSelectedIndex() == Tab.SUBSCRIPTION.getCode())
            {
                showDialog("Edit Subscription", new AddEditSubscritpionDialog(this, true, subscriptionPanel.getSelectedRow()));
                subscriptionPanel.refreshTable();
            }
            else if (tabbedPaneModel.getSelectedIndex() == Tab.SUPPLIER.getCode())
            {
                showDialog("Edit Supplier", new AddEditSupplierDialog(this, true, supplierPanel.getSelectedRow()));
                supplierPanel.refreshTable();
            }
        }
        catch (NoRowSelected ex)
        {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Select a row", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_btnUpdateActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnDeleteActionPerformed
    {//GEN-HEADEREND:event_btnDeleteActionPerformed
        SingleSelectionModel tabbedPaneModel = tabbedPane.getModel();

        try
        {
            if (tabbedPaneModel.getSelectedIndex() == Tab.BILLING.getCode())
            {
                HashMap<String, String> selectedRow = billingPanel.getSelectedRow();
                int askUserQuestion = AppUtils.askUserQuestion(this, "Are you sure you want to delete the selected item?\n", "Deleting", JOptionPane.YES_NO_CANCEL_OPTION);
                if (askUserQuestion == JOptionPane.YES_OPTION)
                {
                    BillingCrudServiceImpl billingCrudService = new BillingCrudServiceImpl();
                    billingCrudService.destroy(Integer.parseInt(selectedRow.get("BILLING NUMBER")));
                    billingPanel.refreshTable();
                }
            }
            else if (tabbedPaneModel.getSelectedIndex() == Tab.CUSTOMER.getCode())
            {
                HashMap<String, String> selectedRow = customerPanel.getSelectedRow();
                int askUserQuestion = AppUtils.askUserQuestion(this, "Are you sure you want to delete the selected item?\n", "Deleting", JOptionPane.YES_NO_CANCEL_OPTION);
                if (askUserQuestion == JOptionPane.YES_OPTION)
                {
                    CustomerCrudServiceImpl customerCrudService = new CustomerCrudServiceImpl();
                    customerCrudService.destroy(Integer.parseInt(selectedRow.get("CUSTOMER NUMBER")));
                    customerPanel.refreshTable();
                }
            }
            else if (tabbedPaneModel.getSelectedIndex() == Tab.DELIVERY.getCode())
            {
                HashMap<String, String> selectedRow = deliveryPanel.getSelectedRow();
                int askUserQuestion = AppUtils.askUserQuestion(this, "Are you sure you want to delete the selected item?\n", "Deleting", JOptionPane.YES_NO_CANCEL_OPTION);
                if (askUserQuestion == JOptionPane.YES_OPTION)
                {
                    DeliveryCrudServiceImpl deliveryCrudService = new DeliveryCrudServiceImpl();
                    deliveryCrudService.destroy(Integer.parseInt(selectedRow.get("DELIVERY NUMBER")));
                    deliveryPanel.refreshTable();
                }
            }
            else if (tabbedPaneModel.getSelectedIndex() == Tab.DRIVER.getCode())
            {
                HashMap<String, String> selectedRow = driverPanel.getSelectedRow();
                int askUserQuestion = AppUtils.askUserQuestion(this, "Are you sure you want to delete the selected item?\n", "Deleting", JOptionPane.YES_NO_CANCEL_OPTION);
                if (askUserQuestion == JOptionPane.YES_OPTION)
                {
                    DriverCrudServiceImpl driverCrudService = new DriverCrudServiceImpl();
                    driverCrudService.destroy(Integer.parseInt(selectedRow.get("DRIVER NUMBER")));
                    driverPanel.refreshTable();
                }
            }
            else if (tabbedPaneModel.getSelectedIndex() == Tab.PRODUCT.getCode())
            {
                HashMap<String, String> selectedRow = productPanel.getSelectedRow();
                int askUserQuestion = AppUtils.askUserQuestion(this, "Are you sure you want to delete the selected item?\n", "Deleting", JOptionPane.YES_NO_CANCEL_OPTION);
                if (askUserQuestion == JOptionPane.YES_OPTION)
                {
                    ProductCrudServiceImpl productCrudService = new ProductCrudServiceImpl();
                    productCrudService.destroy(Integer.parseInt(selectedRow.get("PRODUCT NUMBER")));
                    productPanel.refreshTable();
                }
            }
            else if (tabbedPaneModel.getSelectedIndex() == Tab.ROUTE.getCode())
            {
                HashMap<String, String> selectedRow = routePanel.getSelectedRow();
                int askUserQuestion = AppUtils.askUserQuestion(this, "Are you sure you want to delete the selected item?\n", "Deleting", JOptionPane.YES_NO_CANCEL_OPTION);
                if (askUserQuestion == JOptionPane.YES_OPTION)
                {
                    RouteCrudServiceImpl routeCrudService = new RouteCrudServiceImpl();
                    routeCrudService.destroy(Integer.parseInt(selectedRow.get("ROUTE NUMBER")));
                    routePanel.refreshTable();
                }
            }
            else if (tabbedPaneModel.getSelectedIndex() == Tab.SUBSCRIPTION.getCode())
            {
                HashMap<String, String> selectedRow = subscriptionPanel.getSelectedRow();
                int askUserQuestion = AppUtils.askUserQuestion(this, "Are you sure you want to delete the selected item?\n", "Deleting", JOptionPane.YES_NO_CANCEL_OPTION);
                if (askUserQuestion == JOptionPane.YES_OPTION)
                {
                    SubscriptionCrudServiceImpl subscriptionCrudService = new SubscriptionCrudServiceImpl();
                    subscriptionCrudService.destroy(Integer.parseInt(selectedRow.get("SUBSCRIPTION NUMBER")));
                    subscriptionPanel.refreshTable();
                }
            }
            else if (tabbedPaneModel.getSelectedIndex() == Tab.SUPPLIER.getCode())
            {
                HashMap<String, String> selectedRow = supplierPanel.getSelectedRow();
                int askUserQuestion = AppUtils.askUserQuestion(this, "Are you sure you want to delete the selected item?\n", "Deleting", JOptionPane.YES_NO_CANCEL_OPTION);
                if (askUserQuestion == JOptionPane.YES_OPTION)
                {
                    SupplierCrudServiceImpl supplierCrudService = new SupplierCrudServiceImpl();
                    supplierCrudService.destroy(Integer.parseInt(selectedRow.get("SUPPLIER NUMBER")));
                    supplierPanel.refreshTable();
                }
            }
        }
        catch (NonexistentEntityException ex)
        {
            OutputError.toLog(ex);
            OutputError.toUser(ex);
        }
        catch (NoRowSelected ex)
        {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Select a row", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnExitActionPerformed
    {//GEN-HEADEREND:event_btnExitActionPerformed
        if (AppUtils.askExitConfirm() == JOptionPane.YES_OPTION)
        {
            System.exit(0);
        }
    }//GEN-LAST:event_btnExitActionPerformed

    private void btnPrintActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnPrintActionPerformed
    {//GEN-HEADEREND:event_btnPrintActionPerformed
        SingleSelectionModel tabbedPaneModel = tabbedPane.getModel();
        try
        {
            if (tabbedPaneModel.getSelectedIndex() == Tab.BILLING.getCode())
            {
                billingPanel.printTable();
            }
            else if (tabbedPaneModel.getSelectedIndex() == Tab.CUSTOMER.getCode())
            {
                customerPanel.printTable();
            }
            else if (tabbedPaneModel.getSelectedIndex() == Tab.DELIVERY.getCode())
            {
                deliveryPanel.printTable();
            }
            else if (tabbedPaneModel.getSelectedIndex() == Tab.DRIVER.getCode())
            {
                driverPanel.printTable();
            }
            else if (tabbedPaneModel.getSelectedIndex() == Tab.PRODUCT.getCode())
            {
                productPanel.printTable();
            }
            else if (tabbedPaneModel.getSelectedIndex() == Tab.ROUTE.getCode())
            {
                routePanel.printTable();
            }
            else if (tabbedPaneModel.getSelectedIndex() == Tab.SUBSCRIPTION.getCode())
            {
                subscriptionPanel.printTable();
            }
            else if (tabbedPaneModel.getSelectedIndex() == Tab.SUPPLIER.getCode())
            {
                supplierPanel.printTable();
            }
        }
        catch (PrinterException ex)
        {
            OutputError.toLog(ex);
            OutputError.toUser(ex);
        }
    }//GEN-LAST:event_btnPrintActionPerformed

    private void btnExportActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnExportActionPerformed
    {//GEN-HEADEREND:event_btnExportActionPerformed
        SingleSelectionModel tabbedPaneModel = tabbedPane.getModel();
        TableModel tableModel = null;
        if (tabbedPaneModel.getSelectedIndex() == Tab.BILLING.getCode())
        {
            tableModel = billingPanel.getTableModel();
        }
        else if (tabbedPaneModel.getSelectedIndex() == Tab.CUSTOMER.getCode())
        {
            tableModel = customerPanel.getTableModel();
        }
        else if (tabbedPaneModel.getSelectedIndex() == Tab.DELIVERY.getCode())
        {
            tableModel = deliveryPanel.getTableModel();
        }
        else if (tabbedPaneModel.getSelectedIndex() == Tab.DRIVER.getCode())
        {
            tableModel = driverPanel.getTableModel();
        }
        else if (tabbedPaneModel.getSelectedIndex() == Tab.PRODUCT.getCode())
        {
            tableModel = productPanel.getTableModel();
        }
        else if (tabbedPaneModel.getSelectedIndex() == Tab.ROUTE.getCode())
        {
            tableModel = routePanel.getTableModel();
        }
        else if (tabbedPaneModel.getSelectedIndex() == Tab.SUBSCRIPTION.getCode())
        {
            tableModel = subscriptionPanel.getTableModel();
        }
        else if (tabbedPaneModel.getSelectedIndex() == Tab.SUPPLIER.getCode())
        {
            tableModel = supplierPanel.getTableModel();
        }
        ExcelExporter excelExporter = new ExcelExporter();
        try
        {
            excelExporter.performedExport(tableModel);
        }
        catch (IOException | WriteException | InterruptedException ex)
        {
            OutputError.toLog(ex);
            OutputError.toUser(ex);
        }
    }//GEN-LAST:event_btnExportActionPerformed

    private void btnReportActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnReportActionPerformed
    {//GEN-HEADEREND:event_btnReportActionPerformed
        new ReportFrame();
    }//GEN-LAST:event_btnReportActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnExport;
    private javax.swing.JButton btnPrint;
    private javax.swing.JButton btnReport;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator5;
    private javax.swing.JSeparator jSeparator6;
    private javax.swing.JLabel lblStatusBar;
    private javax.swing.JTabbedPane tabbedPane;
    // End of variables declaration//GEN-END:variables
}